{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "=====================\n",
    "分类比较\n",
    "=====================\n",
    "\n",
    "scikit-learn 中的几个分类器在合成数据集上的比较。\n",
    "这个例子的目的是说明不同分类器的决策边界的性质。\n",
    "这仿佛是大海中的一滴水，因为这些例子所传达的直觉不一定会转移到真正的数据集上。\n",
    "\n",
    "特别是在高维空间中，数据可以更容易地线性分离，诸如朴素贝叶斯和线性 SVM 之类的分类器的简单性可能导致比其他分类器更好的泛化。\n",
    "\n",
    "绘图显示了纯色和测试点半透明的训练点。右下方显示了测试仪的分类精度。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "from matplotlib.colors import ListedColormap\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.preprocessing import StandardScaler\n",
    "from sklearn.datasets import make_moons, make_circles, make_classification\n",
    "\n",
    "#神经网络\n",
    "from sklearn.neural_network import MLPClassifier\n",
    "from sklearn.neighbors import KNeighborsClassifier\n",
    "from sklearn.svm import SVC  #支持向量机\n",
    "from sklearn.gaussian_process import GaussianProcessClassifier\n",
    "from sklearn.gaussian_process.kernels import RBF\n",
    "from sklearn.tree import DecisionTreeClassifier\n",
    "from sklearn.ensemble import RandomForestClassifier, AdaBoostClassifier\n",
    "\n",
    "#一共有3个朴素贝叶斯的分类算法类。分别是GaussianNB，MultinomialNB和BernoulliNB。\n",
    "#其中GaussianNB就是先验为高斯分布的朴素贝叶斯，MultinomialNB就是先验为多项式分布的朴素贝叶斯，\n",
    "#而BernoulliNB就是先验为伯努利分布的朴素贝叶斯。\n",
    "from sklearn.naive_bayes import GaussianNB\n",
    "from sklearn.discriminant_analysis import QuadraticDiscriminantAnalysis\n",
    "\n",
    "h =.02 # 网格中的步长\n",
    "\n",
    "# 各分类器对应的名称\n",
    "# 各分类器为 1.k-近邻，也就是 kNN 2. 线性支持向量机 3. 带有 RBF 核的 SVM 4.高斯过程 5.决策树 6.随机森林 7.神经网络 8.集成方法 AdaBoost 9.朴素贝叶斯 10.二次判别分析\n",
    "names = [\"Nearest Neighbors\", \"Linear SVM\", \"RBF SVM\", \"Gaussian Process\",\n",
    "         \"Decision Tree\", \"Random Forest\", \"Neural Net\", \"AdaBoost\",\n",
    "         \"Naive Bayes\", \"QDA\"]\n",
    "\n",
    "# 具体的分类器参数和调用\n",
    "classifiers = [\n",
    "    KNeighborsClassifier(3),\n",
    "    SVC(kernel=\"linear\", C=0.025),\n",
    "    SVC(gamma=2, C=1),\n",
    "    GaussianProcessClassifier(1.0 * RBF(1.0), warm_start=True),\n",
    "    DecisionTreeClassifier(max_depth=5),\n",
    "    RandomForestClassifier(max_depth=5, n_estimators=10, max_features=1),\n",
    "    MLPClassifier(alpha=1),\n",
    "    AdaBoostClassifier(),\n",
    "    GaussianNB(),\n",
    "    QuadraticDiscriminantAnalysis()]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[(array([[ 0.03159499,  0.98698776],\n",
       "         [ 2.11509784, -0.04624397],\n",
       "         [ 0.88248972, -0.07575606],\n",
       "         [-0.0551441 , -0.03733246],\n",
       "         [ 0.82954503, -0.53932149],\n",
       "         [ 2.11285708,  0.66208353],\n",
       "         [ 0.5696927 ,  0.33744136],\n",
       "         [ 0.95217454, -0.75307471],\n",
       "         [-0.02922115,  0.39287336],\n",
       "         [ 1.71579557,  0.36069454],\n",
       "         [-0.27883014,  0.61420539],\n",
       "         [ 2.00344509, -0.3091496 ],\n",
       "         [ 1.50009616, -0.74636376],\n",
       "         [ 1.22540835,  1.19793017],\n",
       "         [ 0.46730819,  0.54703192],\n",
       "         [-0.36653222,  1.11971633],\n",
       "         [ 0.30124459,  1.23315697],\n",
       "         [ 0.30430746,  0.82373935],\n",
       "         [-0.23712492,  0.77876034],\n",
       "         [ 1.16339832,  0.55290238],\n",
       "         [-0.59702417,  0.6572361 ],\n",
       "         [ 0.46356735, -0.1951543 ],\n",
       "         [ 0.68451111,  0.75287685],\n",
       "         [ 0.70515699, -0.45892444],\n",
       "         [-0.65805008, -0.12944211],\n",
       "         [-0.74662946, -0.3829632 ],\n",
       "         [ 0.60585226,  0.31252842],\n",
       "         [ 2.18137168, -0.02291747],\n",
       "         [ 1.91980633,  0.17247329],\n",
       "         [ 1.4834364 , -0.10517023],\n",
       "         [ 0.47404723, -0.14575067],\n",
       "         [ 1.05614605, -1.03570207],\n",
       "         [ 1.86500732, -0.01107874],\n",
       "         [ 0.4194721 , -0.32478101],\n",
       "         [ 0.06873258,  0.56648467],\n",
       "         [-0.17332432,  1.00215131],\n",
       "         [ 0.12412421,  1.00062892],\n",
       "         [ 0.18121142,  0.24717743],\n",
       "         [-0.25451559,  0.19317272],\n",
       "         [ 1.02580668, -0.62574566],\n",
       "         [ 1.52002143, -0.04515581],\n",
       "         [ 0.64174037, -0.39369468],\n",
       "         [-0.87615589,  0.0465662 ],\n",
       "         [-1.06964997,  0.13666025],\n",
       "         [ 1.02658765,  0.37782458],\n",
       "         [ 0.93131325,  1.38517841],\n",
       "         [ 0.67680544,  1.57189931],\n",
       "         [-0.36885733,  0.72886601],\n",
       "         [-1.02465495,  0.16190215],\n",
       "         [ 0.77455385,  0.15375803],\n",
       "         [ 1.4045116 , -0.00705701],\n",
       "         [-0.38147174, -0.23488747],\n",
       "         [ 0.72155224,  0.44721658],\n",
       "         [-0.51346686,  0.67869095],\n",
       "         [ 0.32118546,  0.28668667],\n",
       "         [ 0.1953628 ,  0.16085107],\n",
       "         [ 0.52824196,  0.98300993],\n",
       "         [-0.2216539 ,  0.25160139],\n",
       "         [ 0.22334676,  1.32217183],\n",
       "         [-0.10704572,  0.56178326],\n",
       "         [ 0.63651685,  0.75444825],\n",
       "         [-0.37227848,  0.99291317],\n",
       "         [ 0.20718083, -0.09767143],\n",
       "         [ 0.12733142, -0.3796549 ],\n",
       "         [ 0.71435231, -0.79994161],\n",
       "         [ 1.09487814, -0.36841845],\n",
       "         [-0.14814362,  0.96158657],\n",
       "         [ 1.586188  , -0.62984517],\n",
       "         [ 0.74444551,  0.57661371],\n",
       "         [ 2.18011028, -0.69977751],\n",
       "         [ 0.24575594,  0.8496383 ],\n",
       "         [-0.95003581,  0.90361699],\n",
       "         [-0.88230758,  0.07249044],\n",
       "         [ 2.02297079,  0.12325148],\n",
       "         [ 2.14577321,  0.46296362],\n",
       "         [ 0.35536468, -0.67847989],\n",
       "         [ 0.34665026,  1.11570676],\n",
       "         [ 1.7392373 ,  0.45900352],\n",
       "         [ 0.63856467, -0.44718443],\n",
       "         [ 1.2876687 , -0.4910366 ],\n",
       "         [-0.13772607,  1.2453262 ],\n",
       "         [-0.56175303,  1.05486051],\n",
       "         [ 1.29003748, -0.20691405],\n",
       "         [-0.87539365,  0.50543423],\n",
       "         [-0.92858249, -0.45631991],\n",
       "         [ 0.02493632,  0.10747958],\n",
       "         [ 0.1972559 , -0.06801668],\n",
       "         [ 0.73346056,  0.28161929],\n",
       "         [ 1.68294434, -0.2020423 ],\n",
       "         [ 0.50764124, -0.11731979],\n",
       "         [ 1.66760217, -0.42485665],\n",
       "         [-0.82172282,  0.63141066],\n",
       "         [ 0.30170903,  0.78603534],\n",
       "         [ 1.37671505, -0.80915107],\n",
       "         [ 1.17037551,  0.59840653],\n",
       "         [ 1.69945309,  0.58771967],\n",
       "         [ 0.21862323, -0.65252119],\n",
       "         [ 0.95291428, -0.41976564],\n",
       "         [-1.31850034,  0.42311235],\n",
       "         [-1.29681764,  0.18414709]]),\n",
       "  array([0, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0,\n",
       "         1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0, 1, 0, 0, 0, 0,\n",
       "         0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 0, 1, 0,\n",
       "         1, 0, 0, 0, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0,\n",
       "         0, 1, 0, 1, 1, 1, 0, 0], dtype=int64)),\n",
       " (array([[-0.38289117, -0.09084004],\n",
       "         [-0.02096229, -0.47787416],\n",
       "         [-0.39611596, -1.28942694],\n",
       "         [-0.61813018, -0.06383715],\n",
       "         [ 0.70347838, -0.18703837],\n",
       "         [-0.45970463,  0.69477465],\n",
       "         [-0.45091682, -0.71570524],\n",
       "         [-0.45562004, -0.13406016],\n",
       "         [-0.26513904,  0.40812871],\n",
       "         [-0.15474648,  0.41406973],\n",
       "         [ 0.231206  , -0.53275899],\n",
       "         [ 0.15623875, -0.8678088 ],\n",
       "         [ 0.51647541,  0.48940995],\n",
       "         [ 0.68707007, -0.02334129],\n",
       "         [ 0.54759869, -0.16482373],\n",
       "         [-0.32179572, -0.80553536],\n",
       "         [-1.07478639, -0.733362  ],\n",
       "         [ 0.76758455, -0.43498783],\n",
       "         [-0.47475234, -0.33813186],\n",
       "         [ 0.15420656,  1.07306032],\n",
       "         [ 0.65916696,  0.20773634],\n",
       "         [-0.77795003,  0.1326555 ],\n",
       "         [ 0.47025454, -0.31228748],\n",
       "         [-0.04246799,  0.26555446],\n",
       "         [-0.72405954,  0.48807185],\n",
       "         [-0.36960005, -1.06514028],\n",
       "         [ 0.17833327, -0.49718972],\n",
       "         [-0.93927864, -0.41951638],\n",
       "         [ 0.50914152, -0.70977467],\n",
       "         [-0.05569852, -0.82162607],\n",
       "         [-0.11214579,  0.72197044],\n",
       "         [ 0.80463921, -0.15221296],\n",
       "         [ 0.08261487, -0.11749021],\n",
       "         [ 0.20349541, -0.37396789],\n",
       "         [ 0.13864693, -0.23905642],\n",
       "         [ 0.32785307, -1.00769037],\n",
       "         [ 0.88944061, -0.39117628],\n",
       "         [-0.05837947,  0.28487039],\n",
       "         [-1.0673653 ,  0.2204006 ],\n",
       "         [-0.60071345, -0.69545189],\n",
       "         [-0.03972324, -0.40936056],\n",
       "         [ 0.39742085,  0.20621162],\n",
       "         [-0.36941154,  0.0129811 ],\n",
       "         [ 0.03573703,  0.46666229],\n",
       "         [-0.56814999, -0.41288419],\n",
       "         [ 0.41047299, -0.73640868],\n",
       "         [ 0.88249707, -0.69004404],\n",
       "         [ 0.06579822, -0.50458395],\n",
       "         [-0.75737223, -0.0724028 ],\n",
       "         [ 0.18316966,  0.08722007],\n",
       "         [ 0.67248314, -0.41892665],\n",
       "         [ 0.25898723,  0.39688645],\n",
       "         [-1.1312983 ,  0.4810614 ],\n",
       "         [ 1.0592844 ,  0.64490287],\n",
       "         [ 0.41019663,  0.38790198],\n",
       "         [ 0.95142029, -0.04089983],\n",
       "         [-0.60492988,  0.43950906],\n",
       "         [ 0.23314762, -0.81785711],\n",
       "         [ 0.91067331,  0.30702075],\n",
       "         [-0.45026472, -0.03724104],\n",
       "         [-0.81396121, -0.64733959],\n",
       "         [-0.23191338,  0.50533992],\n",
       "         [-0.59760983,  0.28023168],\n",
       "         [ 0.73960166, -0.84270281],\n",
       "         [ 0.57294659, -0.31198928],\n",
       "         [ 0.24821133, -0.54784509],\n",
       "         [ 0.52127802,  0.94108005],\n",
       "         [ 0.33973198,  0.10609978],\n",
       "         [ 1.05339036, -0.02197593],\n",
       "         [ 0.01327466, -0.63379502],\n",
       "         [ 0.2422589 ,  0.49032064],\n",
       "         [-0.89266612,  0.6345076 ],\n",
       "         [ 0.1672566 ,  0.23548462],\n",
       "         [-0.05611705,  0.38834099],\n",
       "         [ 0.84695486,  0.81435811],\n",
       "         [ 0.29976195, -0.07943031],\n",
       "         [-0.1404762 ,  0.72486032],\n",
       "         [-0.05482024,  0.18417328],\n",
       "         [-0.24643884, -0.43283337],\n",
       "         [-0.23460645,  0.6409442 ],\n",
       "         [-1.13184893, -0.61964942],\n",
       "         [-0.92413821, -0.45302089],\n",
       "         [ 0.2225745 ,  0.77052597],\n",
       "         [-0.69453765,  0.53014147],\n",
       "         [-1.0362509 ,  0.77339965],\n",
       "         [ 0.51880585,  0.30152232],\n",
       "         [-0.77429541,  0.02553767],\n",
       "         [ 0.71468326,  0.56869015],\n",
       "         [-0.33875274,  0.46826063],\n",
       "         [-0.34749244,  0.13441418],\n",
       "         [ 1.12980796,  0.04281936],\n",
       "         [-0.38308979,  0.79116812],\n",
       "         [-0.07425141,  0.2184625 ],\n",
       "         [-0.44945202, -0.05722266],\n",
       "         [ 0.85783288,  0.63778888],\n",
       "         [-0.47486203, -0.22498112],\n",
       "         [ 0.12627243,  0.86978412],\n",
       "         [-0.64736458, -0.36342437],\n",
       "         [ 0.47440459,  1.01101585],\n",
       "         [-0.38565772, -0.81031183]]),\n",
       "  array([1, 1, 0, 1, 1, 0, 0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1, 1,\n",
       "         1, 0, 0, 1, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 1, 1, 1, 0, 1, 0,\n",
       "         0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 0, 0, 1, 0, 1, 0,\n",
       "         1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0,\n",
       "         1, 1, 0, 1, 0, 1, 0, 0], dtype=int64)),\n",
       " (array([[-0.17749658,  0.93049627],\n",
       "         [ 1.97742406,  1.76615529],\n",
       "         [ 1.80002379,  1.7003428 ],\n",
       "         [-0.77083686,  2.35916348],\n",
       "         [-0.30800901,  1.59406316],\n",
       "         [ 0.61472141,  2.43489824],\n",
       "         [ 1.0909431 ,  2.21663402],\n",
       "         [-0.83488954,  2.35868875],\n",
       "         [ 2.47946499,  1.33418967],\n",
       "         [ 1.19926597,  1.82681392],\n",
       "         [ 2.18506184,  0.8226875 ],\n",
       "         [ 1.68423203,  1.0411311 ],\n",
       "         [ 1.14621135,  2.07048527],\n",
       "         [ 1.35596945,  0.56802498],\n",
       "         [ 1.38520748,  1.82607238],\n",
       "         [ 1.86729217,  1.18692699],\n",
       "         [ 0.31227376,  2.24605353],\n",
       "         [-0.08183484,  1.70065511],\n",
       "         [ 0.90484472,  2.60683016],\n",
       "         [-0.4358609 ,  2.0151414 ],\n",
       "         [ 0.81441196,  2.13546784],\n",
       "         [ 3.10874312,  2.26786504],\n",
       "         [-0.25574217,  1.71281146],\n",
       "         [ 1.88663027,  2.72089513],\n",
       "         [ 2.29503547,  3.89267759],\n",
       "         [-0.11415626,  0.85593357],\n",
       "         [ 1.69703537,  3.47717799],\n",
       "         [ 1.83855775,  0.92667213],\n",
       "         [ 1.60298807,  1.3018436 ],\n",
       "         [ 1.13803215,  2.82955007],\n",
       "         [ 0.57002738,  1.82202469],\n",
       "         [ 0.25130172,  1.32225048],\n",
       "         [-0.39833078,  1.984345  ],\n",
       "         [ 1.6291326 ,  0.88168182],\n",
       "         [ 3.10503585,  1.74000633],\n",
       "         [-0.00566874,  1.12039041],\n",
       "         [ 1.36866557,  2.13964438],\n",
       "         [ 2.69839961,  3.54932382],\n",
       "         [ 2.00170838,  1.09862314],\n",
       "         [ 2.43627708,  1.43769476],\n",
       "         [-0.30996938,  0.91256444],\n",
       "         [ 0.08528148,  1.72286096],\n",
       "         [ 2.06235803,  1.60351529],\n",
       "         [ 1.61026984,  2.06899308],\n",
       "         [ 1.39875914,  0.69295034],\n",
       "         [ 1.68417195,  1.05499076],\n",
       "         [ 0.6032637 ,  2.18625056],\n",
       "         [-0.80090405,  1.89873999],\n",
       "         [ 2.28622906,  2.00485199],\n",
       "         [-1.08107738,  2.77507784],\n",
       "         [ 0.6731465 ,  1.08374453],\n",
       "         [ 2.96970576,  2.12322913],\n",
       "         [ 0.67950668,  2.09864274],\n",
       "         [-0.44781177,  3.18420372],\n",
       "         [-0.50033854,  2.02105531],\n",
       "         [ 1.85307118,  3.66612496],\n",
       "         [ 1.72186424,  1.78055874],\n",
       "         [ 2.25033483,  3.12143112],\n",
       "         [ 0.31356908,  1.35127949],\n",
       "         [-0.28410691,  1.85727304],\n",
       "         [ 2.12915656,  2.58463127],\n",
       "         [ 1.5764227 ,  2.01987282],\n",
       "         [-1.07819549,  1.40147405],\n",
       "         [ 1.19768119,  2.40249075],\n",
       "         [ 1.37690112,  1.14985842],\n",
       "         [-1.66006488,  1.46313474],\n",
       "         [ 1.24390011,  1.46004628],\n",
       "         [ 1.87922305,  1.72262314],\n",
       "         [ 2.90850791,  2.94159145],\n",
       "         [-0.48122613,  1.79143762],\n",
       "         [ 2.4374304 ,  1.34535331],\n",
       "         [ 2.56579874,  0.99334358],\n",
       "         [ 2.44899241,  2.49072727],\n",
       "         [ 0.46034247,  0.68402612],\n",
       "         [-0.39068268,  2.13223301],\n",
       "         [-1.26676594,  1.15606381],\n",
       "         [ 1.41882694,  2.57154527],\n",
       "         [-0.56760436,  0.99010229],\n",
       "         [-1.80173745,  1.23149132],\n",
       "         [ 0.18035906,  1.25587797],\n",
       "         [ 2.85271766,  1.84520561],\n",
       "         [ 1.84588723,  1.15083733],\n",
       "         [ 2.2332857 ,  3.1528139 ],\n",
       "         [-0.55018044,  1.08169147],\n",
       "         [-0.79089021,  1.65085521],\n",
       "         [-0.13199329,  1.94007796],\n",
       "         [ 0.70257257,  1.35701296],\n",
       "         [ 0.27672155,  2.67298668],\n",
       "         [ 0.61021587,  2.1651486 ],\n",
       "         [-0.53000368,  2.01943164],\n",
       "         [ 1.83727888,  2.1110129 ],\n",
       "         [ 0.28416019,  2.64492964],\n",
       "         [ 1.89688379,  2.26044693],\n",
       "         [ 2.06813613,  2.6101669 ],\n",
       "         [-1.72274127,  1.27907652],\n",
       "         [ 2.63238161,  1.27130508],\n",
       "         [-0.04025574,  1.7827081 ],\n",
       "         [-0.78745318,  1.40035688],\n",
       "         [ 2.70244116,  1.58744358],\n",
       "         [ 1.2909693 ,  2.75193673]]),\n",
       "  array([1, 0, 0, 1, 1, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1,\n",
       "         0, 0, 1, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0,\n",
       "         1, 1, 0, 1, 1, 0, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0,\n",
       "         1, 0, 0, 0, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1,\n",
       "         0, 0, 1, 0, 1, 1, 0, 1]))]"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X, y = make_classification(n_features=2, n_redundant=0, n_informative=2,\n",
    "                           random_state=1, n_clusters_per_class=1)\n",
    "\n",
    "#此命令将会产生一个随机状态种子\n",
    "rng = np.random.RandomState(2)\n",
    "X += 2 * rng.uniform(size=X.shape)\n",
    "linearly_separable = (X, y)\n",
    "\n",
    "datasets = [make_moons(noise=0.3, random_state=0),\n",
    "            make_circles(noise=0.2, factor=0.5, random_state=1),\n",
    "            linearly_separable\n",
    "            ]\n",
    "datasets"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "figure = plt.figure(figsize=(27,9))\n",
    "i = 1\n",
    "\n",
    "for ds_cnt, ds in enumerate(datasets):\n",
    "    "
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
